"""
ip138poster.py
Created by Luke on 2009-05-04.
Copyright (c) 2009 Alibaba. All rights reserved.
"""
  
import httplib,urllib;  #加载模块  
import re

class Ip138Poster:
	
	def __init__(self):
		#定义一些文件头  
		self.headers = {"Content-Type":"application/x-www-form-urlencoded",  
           			"Connection":"Keep-Alive","Referer":"http://www.ip138.com/ips.asp"};  
		#与网站构建一个连接  
		self.conn = httplib.HTTPConnection("www.ip138.com");

	def post(self,ip_addr):
		#定义需要进行发送的数据 
		params = urllib.urlencode({'ip':ip_addr,'action':'2'});
		#开始进行数据提交   同时也可以使用get进行  
		self.conn.request(method="POST",url="/ips8.asp",body=params,headers=self.headers);  
		#返回处理后的数据  
		response = self.conn.getresponse();  
		#判断是否提交成功  
		if response.status == 200:
			s=response.read()
			print s.decode("gbk")
			lis = re.findall(r"(?<=<li>).*?(?=</li>)",s)
			return lis
	
	def get_main_data(self,ip_addr):
		##下面的十六进制的值为"本站主数据："的unicode
		self.main_data_parttern = r"(?<=\xe6\x9c\xac\xe7\xab\x99\xe4\xb8\xbb\xe6\x95\xb0\xe6\x8d\xae\xef\xbc\x9a).*"
		list = self.post(ip_addr)
		if len(list)>0:
			main_data = re.findall(self.main_data_parttern,list[0].decode("gbk").encode("utf8"))
		if len(main_data)>0:
			ret_value = main_data[0]
		return ret_value.decode("utf8")
		
		
			
	def __del__(self):
		#关闭连接  
		self.conn.close()



##由于需要匹配中文，所以需要转换，然后再匹配
def UTF2Hex(s):
    temp = s.encode("UTF-8").encode("hex")
    line = ""
    for i in range(0,len(temp)-1,2):
        line += "\\x" + temp[i] + temp[i+1]
    return line

def GBK2Hex(s):
    temp = s.encode("GBK").encode("hex")
    line = ""
    for i in range(0,len(temp)-1,2):
        line += "\\x" + temp[i] + temp[i+1]
    return line


if __name__ == '__main__':
	poster = Ip138Poster()
	print poster.get_main_data("121.0.29.231")